package com.hsytech.baoinbao.base;

import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hsytech.baoinbao.R;
import com.hsytech.baoinbao.volleytool.HttpUtil;

/**
 * Created by 123 on 2016/3/10.
 */
public class BaseActivity extends FragmentActivity {
    private ViewGroup contentView;

    private RelativeLayout bodyContainer;

    private LayoutInflater inflater;

    private TextView center_title, right_button_text;
    private ImageButton left_button;
    private LinearLayout right_button;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View view = this.getCurrentFocus();
        if (view != null) {
            IBinder a = view.getWindowToken();
            if (a != null) {
                return imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
            }
        }


        return super.onTouchEvent(event);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = getLayoutInflater();
        contentView = (ViewGroup) inflate(R.layout.base_activity_with_simple_actionbar);
        super.setContentView(contentView);
        bodyContainer = (RelativeLayout) contentView.findViewById(R.id.body);
        center_title = (TextView) contentView.findViewById(R.id.center_title);
        left_button = (ImageButton) contentView.findViewById(R.id.left_button);
        left_button.setImageResource(R.mipmap.left_button);
        left_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        right_button = (LinearLayout) contentView.findViewById(R.id.right_button);
        right_button_text = (TextView) contentView.findViewById(R.id.right_button_text);
        right_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onMenuRightBtn();
            }
        });
    }

    public void onMenuRightBtn() {
    }

    public void setRightBtn(boolean visibility) {
        if (visibility) {
            this.right_button.setVisibility(View.VISIBLE);
        } else {
            this.right_button.setVisibility(View.GONE);

        }
    }

    public void setRight_button_text(String text) {
        this.right_button_text.setText(text);
    }

    public void setLeft_button(boolean visibility) {
        if (visibility) {
            this.left_button.setVisibility(View.VISIBLE);
        } else {
            this.left_button.setVisibility(View.GONE);

        }
    }

    public void setCenterTitleText(String title) {
        this.center_title.setText(title);
    }

    public View inflate(int layoutRes) {
        return inflater.inflate(layoutRes, null);
    }


    @Override
    public void setContentView(int layoutResID) {
        bodyContainer.addView(inflate(layoutResID), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(View view) {
        bodyContainer.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (null == params) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        bodyContainer.addView(view, params);
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        contentView.addView(view, params);
    }

    public void addContentView(View view, int index, ViewGroup.LayoutParams params) {
        contentView.addView(view, index, params);
    }


    public <T extends View> T F(int resId) {
        return (T) findViewById(resId);
    }

    public <T extends View> T F(View parent, int resId) {
        return (T) parent.findViewById(resId);
    }

    private Toast toast = null;

    public void showToast(String msg) {
        if (null == toast) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public void showToast(int resId) {
        showToast(getString(resId));
    }

    public boolean isEmpty(String string) {
        return null == string || "".equals(string);
    }

    public <T> boolean isEmpty(T[] arr) {
        if (null == arr) {
            return true;
        }
        for (int i = 0; i < arr.length; i++) {
            if (null != arr[i]) {
                return false;
            }
        }
        return true;
    }

}
